class Tools::FlashsController < ApplicationController
  before_filter :authorize, :except => [:index_in_show,:show]
  layout 'base.html', :only => :show
  
  def index
    @setting = UserToolSetting.find(params[:setting_id])
  end
  
  def show
    @tool = Tool.find(params[:id])
    @seo_title = "麦吐司网-#{@tool.name}-#{@tool.synopsis}-#{@tool.key_words.split(' ').join(',')}"
    @seo_keywords = @tool.key_words.split(' ').join(',')
    @seo_description = @tool.synopsis
  end
  
  def destroy
    @setting = UserToolSetting::Flash.find(params[:id])
    render :text => '', :status => @setting.destroy ? 200 : 500
  end
  
  def index_in_show
    @tool = Tool.find_by_code(params[:tool_code])
    @settings = UserToolSetting::Flash.find_all_by_user_id_and_tool_id(current_user_id, @tool.id)
  end
  
  def new_in_show
    @tool = Tool.find(params[:tool_id])
    @province = Province.first
    @setting = UserToolSetting::Flash.new(
      :column => @tool.default_column,
      :tool_id => @tool.id
    )
  end
  
  def create_in_show    
    @tool = Tool.find(params[:tool_id])
    @setting = UserToolSetting::Flash.new(params[:user_tool_setting_flash])
    @setting.attributes = {
      :user_id => current_user_id,
      :tool_id => @tool.id
    }
    if @setting.save
      redirect_to :action => :index_in_show, :tool_code => @tool.code , :format => :js
    else
      @province = Province.first
      render :new_in_show
    end
  end
  
  def edit_in_show    
    @setting = UserToolSetting::Flash.find(params[:id])
    @tool = @setting.tool
  end
  
  def update_in_show
    @setting = UserToolSetting::Flash.find(params[:id])
    @tool = @setting.tool
    if @setting.update_attributes(params[:user_tool_setting_flash])
      redirect_to :action => :index_in_show, :tool_code => @tool.code , :format => :js
    else
      @province = Province.first
      render :edit_in_show
    end
  end
  
  def destroy_in_show
    @setting = UserToolSetting::Flash.find(params[:id])
    render :text => '', :status => @setting.destroy ? 200 : 500
  end
  
  def add_flash_to_my_dish
    @tool = Tool.find(params[:tool_id])
    @setting = UserToolSetting::Flash.new(
      :user_id => current_user_id,
      :tool_id => @tool.id
    )
    @setting.save!
    render :text => '成功'
  end
end
